<?php
namespace Tbf\Bundle\UploadBundle\Service;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Validation;

class Verify
{
    /**
     * @param $name
     * @param  UploadedFile $file
     * @return error
     */
    public function verify($name,UploadedFile $file)
    {
        $validator = Validation::createValidator();
        $violations = $validator->validateValue($file, new File(array(
            'mimeTypes' => array(
                'image/jpeg',
                'image/png',
                'image/gif',
            ),
            'mimeTypesMessage' => '上传文件类型不允许！',
        )));
        if (count($violations) != 0) {
            return $violations[0]->getMessage();
        }

        return null;
    }
}
